#include <stdio.h>
#include <stdlib.h>

#include "gtk/gtk.h"
//#include "notebook.h"

void menu_item_response(GtkWidget *widget,gpointer data);

gint delete_event(GtkWidget *widget,
                    gpointer data)
{
    g_print("porgrame quit succesfully\n");
    return FALSE;
}

void destory_event(GtkWidget *widget,
                    GdkEvent *event,
                    gpointer data)
{
    gtk_main_quit();
}


int main(int argc,
               char* argv[])
{
    GtkWidget *notebk_win;
    GtkWidget *vbox1;
    GtkWidget *textArea;
    GtkWidget *statusBar;
    GtkWidget *menuBar;
    GtkWidget *menu;
    //GtkWidget *button;
    GtkWidget *item;
    GtkWidget *openItem,*saveItem,*quitItem;

    //init window for receive Natuils event
    gtk_init(&argc,&argv);

    //create notebook main window
    notebk_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position (GTK_WINDOW(notebk_win), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(notebk_win),
                                 800,
                                 600);
    gtk_window_set_icon_from_file (GTK_WINDOW(notebk_win),"kcmkicker.ico",NULL);
    g_signal_connect(G_OBJECT(notebk_win),"delete_event",
                     G_CALLBACK(delete_event),NULL);
    g_signal_connect(G_OBJECT(notebk_win),"destroy",
                     G_CALLBACK(destory_event),NULL);
   
    //create vbox,and put menubar,textarea,status bar into it;
    vbox1 = gtk_vbox_new(FALSE,0);
    gtk_container_set_border_width(GTK_CONTAINER(vbox1),10);

    menuBar = gtk_menu_bar_new();
    //gtk_container_add(GTK_CONTAINER(vbox1),menuBar);
    gtk_box_pack_start(GTK_BOX(vbox1),menuBar,FALSE,FALSE,10);
    gtk_widget_show(menuBar);
   
    menu = gtk_menu_new();

    openItem = gtk_menu_item_new_with_label("Open");
    gtk_menu_append(GTK_MENU(menu),openItem);
    gtk_widget_show(openItem);

    saveItem = gtk_menu_item_new_with_label("Save");
    gtk_menu_append(GTK_MENU(menu),saveItem);
    gtk_widget_show(saveItem);

    quitItem = gtk_menu_item_new_with_label("Quit");
    gtk_menu_append(GTK_MENU(menu),quitItem);
    gtk_widget_show(quitItem);

    g_signal_connect_swapped (G_OBJECT (quitItem), "activate",
                          G_CALLBACK (menu_item_response),
                          NULL);

    item = gtk_menu_item_new_with_label("File");
    gtk_widget_show(item);

    gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),menu);
    gtk_menu_bar_append(GTK_MENU_BAR(menuBar),item);
    /*
    button = gtk_button_new_with_label("File");
    gtk_menu_shell_prepend(menuBar,button);
    gtk_widget_show(button);
    */

    textArea = gtk_text_view_new();
    gtk_box_pack_start(GTK_BOX(vbox1),textArea,TRUE,TRUE,0);
    gtk_widget_show(textArea);

    statusBar = gtk_statusbar_new();
    gtk_box_pack_start(GTK_BOX(vbox1),statusBar,FALSE,TRUE,0);
    gtk_widget_show(statusBar);
   
    gtk_container_add(GTK_CONTAINER(notebk_win),vbox1);
    gtk_widget_show(vbox1);
    gtk_widget_show(notebk_win);

    gtk_main();

    return 0;
}

void menu_item_response(GtkWidget *widget,gpointer data)
{
    g_print("button pressed\n");
}

static gint button_pressed(GtkWidget *widget,
                            GdkEvent *event)
{
    if(event->type == GDK_BUTTON_PRESS)
    {
        GdkEventButton *bevent = (GdkEventButton*)event    ;
        gtk_menu_popup(GTK_MENU(widget),NULL,NULL,NULL,NULL,
                    bevent->button,bevent->time);
        return TRUE;
    }
    return FALSE;
}
